package com.yuyu.qingmangmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyu.common.utils.PageUtils;
import com.yuyu.common.utils.Query;
import com.yuyu.qingmangmall.product.dao.BrandDao;
import com.yuyu.qingmangmall.product.dao.CategoryBrandRelationDao;
import com.yuyu.qingmangmall.product.dao.CategoryDao;
import com.yuyu.qingmangmall.product.entity.BrandEntity;
import com.yuyu.qingmangmall.product.entity.CategoryBrandRelationEntity;
import com.yuyu.qingmangmall.product.entity.CategoryEntity;
import com.yuyu.qingmangmall.product.service.BrandService;
import com.yuyu.qingmangmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        // 查找 对应的brand
        BrandEntity brandEntity = brandDao.selectById(brandId);
        // 查找 对应的category
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        // 查找 对应的brand name
        // @NotBlank(message = "品牌名不能为空", groups = {UpdateGroup.class, AddGroup.class})
        String brandName = brandEntity.getName();
        // 查找 对应的category name
        String categoryName = categoryEntity.getName();

        // 将对应的属性名写入
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);

        // 使用当前service自带的save方法保存关联关系 由Iservice（mybatis-plus）提供
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(name);
        // .eq("brand_id",brandId) 设置更新条件
        this.update(categoryBrandRelation,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 级联更新所有关联的数据
     * @param catId
     * @param name
     */
    @Transactional // 事务
    @Override
    public void updateCategory(Long catId,String name) {
//        this.baseMapper.updateCategory(catId,name);
        CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
        relation.setCatelogName(name);
        relation.setCatelogId(catId);
//
        this.update(relation,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

    @Override
    public List<BrandEntity> getRelation(Long catId) {

        List<CategoryBrandRelationEntity> catelogId = this.baseMapper
                .selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandEntity> collect = catelogId.stream().map(item -> {

            return brandService.getById(item.getBrandId());

        }).collect(Collectors.toList());

        return collect;
    }

}